home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / VOXRAY.ZIP / RCBITMAP.H < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-10  |  5.6 KB  |  219 lines

  1. #include <stddef.h>
  2. #include <dos.h>
  3. #include "rcbitmap.h"
  4.  
  5. rcbitmap * bitmap_struct;
  6. fstream inpcxfile;
  7. fstream outpcxfile;
  8.  
  9. struct pcx_header {
  10.   CHAR manufacturer;    // Always set to 0
  11.   CHAR version;         // Always 5 for 256-color files
  12.   CHAR encoding;        // Always set to 1
  13.   CHAR bits_per_pixel;  // Should be 8 for 256-color files
  14.   SHORT  xmin,ymin;       // Coordinates for top left corner
  15.   SHORT  xmax,ymax;       // Width and height of image
  16.   SHORT  hres;            // Horizontal resolution of image
  17.   SHORT  vres;            // Vertical resolution of image
  18.   CHAR palette16[48];   // EGA palette; not used for
  19.             //  256-color files
  20.   CHAR reserved;        // Reserved for future use
  21.   CHAR color_planes;    // Color planes
  22.   SHORT  bytes_per_line;  // Number of bytes in 1 line of
  23.             //  pixels
  24.   SHORT  palette_type;    // Should be 2 for color palette
  25.   CHAR filler[58];      // Nothing but junk
  26. };
  27.  
  28. LONG Bitmap_In_File(PCHAR filename,ULONG locate)
  29. {
  30.   if (inpcxfile.is_open())
  31.     inpcxfile.close();
  32.   inpcxfile.open(filename,ios::binary | ios::in);
  33.   if  (inpcxfile.fail())
  34.     return -1;
  35.   else {inpcxfile.seekg(locate,ios::beg);
  36.      return 0;}
  37. }
  38.  
  39. LONG Bitmap_Out_File(PCHAR filename,ULONG locate)
  40. {
  41.   if (outpcxfile.is_open())
  42.     outpcxfile.close();
  43.   outpcxfile.open(filename,ios::binary | ios::out | ios::truncate);
  44.   if  (outpcxfile.fail())
  45.     return -1;
  46.   else {outpcxfile.seekp(locate,ios::beg);
  47.      return 0;}
  48. }
  49.  
  50. LONG Setup_Pcx_Read()
  51. {
  52. if (!inpcxfile.is_open())
  53.   return -1;
  54. LONG seek=inpcxfile.tellg();
  55. inpcxfile.seekg(0L,ios::beg);
  56. pcx_header header;
  57. inpcxfile.read((PCHAR)&header,128);
  58. bitmap_struct->image_size=((LONG)header.bytes_per_line)*((LONG)header.color_planes)
  59. *((LONG)(1+header.ymax-header.ymin));
  60. bitmap_struct->width=1+header.xmax-header.xmin;
  61. bitmap_struct->height=1+header.ymax-header.ymin;
  62. inpcxfile.seekg(seek,ios::beg);
  63. return 0;
  64. }
  65.  
  66. LONG Setup_Pcx_Write()
  67. {        
  68. if (!outpcxfile.is_open())
  69.   return -1;
  70. pcx_header header;
  71. header.manufacturer=10;
  72. header.version=5;
  73. header.encoding=1;
  74. header.bits_per_pixel=8;
  75. header.xmin=0;header.ymin=0;
  76. header.xmax=bitmap_struct->width-1;header.ymax=bitmap_struct->height-1;
  77. header.hres=300; header.vres=300;
  78. header.palette16[0]=1;
  79. header.palette16[1]=0;
  80. header.palette16[2]=1;
  81. header.palette16[3]=0;
  82. header.palette16[4]=1;
  83. header.reserved=0;
  84. header.color_planes=1;
  85. header.bytes_per_line=bitmap_struct->width;
  86. header.palette_type=1;
  87. outpcxfile.write((PCHAR)&header,sizeof(header));
  88. return 0;
  89. }
  90.  
  91. LONG Write_Pcx()
  92. {
  93.    if (!outpcxfile.is_open())
  94.       return (-1);
  95.    length=0;
  96.    static UCHAR buffer[5*1024];
  97.    PUCHAR image_ptr=bitmap_struct->image;
  98.    char mode=1;
  99.    static UCHAR writeout;
  100.    static UCHAR bytecount;
  101.    LONG bufptr=0;
  102.    LONG buflen=5*1024;
  103.    LONG i=0;
  104.    while (i<bitmap_struct->image_size) {
  105.        if (mode>0) {
  106.       if (bufptr>=buflen) {
  107.       length+=5*1024;
  108.          bufptr=0;
  109.          outpcxfile.write(buffer,5*1024);}
  110.       writeout=*(image_ptr++);
  111.       if (((bitmap_struct->image_size-i>1)&&(writeout==*(image_ptr)))||(writeout > 0xbf)) {
  112.          bytecount=1;
  113.          mode=0;
  114.       } else { buffer[bufptr++]=writeout; i++;}
  115.        }
  116.        else {
  117.        if ((bitmap_struct->image_size-i>1)&&
  118.         (bytecount<0x39)&&(*(image_ptr)==writeout)){
  119.         bytecount++;
  120.         image_ptr++;}
  121.       else {
  122.         buffer[bufptr++]=bytecount+0xc0;
  123.         if (bufptr>=buflen) {
  124.            length+=5*1024;
  125.            bufptr=0;
  126.            outpcxfile.write(buffer,buflen);}
  127.         buffer[bufptr++]=writeout;
  128.         mode=1;
  129.       }
  130.              i++;
  131.        }
  132.    }
  133. outpcxfile.write(buffer,bufptr);
  134. length+=bufptr;
  135. return 0;
  136. }
  137.      
  138. LONG Load_Pcx()
  139. {
  140.   length=0;
  141.   if (!inpcxfile.is_open())
  142.     return (-1);
  143.   if ((bitmap_struct->image=new UCHAR [bitmap_struct->image_size])==NULL){
  144.     delete bitmap_struct->image;
  145.     return 1;}
  146.   LONG seek=inpcxfile.tellg();
  147.   inpcxfile.seekg(128L,ios::cur);
  148.   static UCHAR buffer[5*1024];
  149.   PUCHAR image_ptr=bitmap_struct->image;
  150.   CHAR mode=1;
  151.   LONG bufptr=0;
  152.   LONG buflen=0;
  153.   static UCHAR readin;
  154.   static UCHAR bytecount;
  155.   for (LONG i=0;i<bitmap_struct->image_size;i++){
  156.     if (mode > 0)
  157.        {
  158.      if (bufptr>=buflen)
  159.         {bufptr=0;
  160.         inpcxfile.read(buffer,5*1024);
  161.                buflen=inpcxfile.gcount();
  162.                length+=buflen;
  163.         if (buflen==0)
  164.           break;} //if (bufptr>=buflen)
  165.      readin=buffer[bufptr++];
  166.      if (readin>0xbf)
  167.        {
  168.        bytecount= (LONG)((LONG)readin & 0x3f);
  169.        if (bufptr>=buflen)
  170.         {bufptr=0;
  171.                inpcxfile.read(buffer,5*1024);
  172.                buflen=inpcxfile.gcount();
  173.                length+=buflen;
  174.         if (buflen==0)
  175.           break;} //if (bufptr>=buflen)
  176.        readin=buffer[bufptr++];
  177.        if (--bytecount > 0) mode=0;
  178.        } //if (readin>0xbf)
  179.        }  //if (mode)
  180.     else if (--bytecount == 0) mode=1;
  181.     *image_ptr++=readin;
  182.   } //for loop
  183.   inpcxfile.clear();
  184.   inpcxfile.seekg(seek,ios::beg);
  185.   return 0;
  186. }
  187.  
  188. LONG Load_Bitmap(char * filename, ULONG locate)
  189. {
  190.   Bitmap_In_File(filename, locate);
  191.   Setup_Pcx_Read();
  192.   if (Read_Pcx()) {
  193.     inpcxfile.close();
  194.     return -1;}
  195.   inpcxfile.close();
  196.   return 0;
  197. }
  198.  
  199. LONG Write_Bitmap(char * filename, ULONG locate)
  200. {
  201.   Bitmap_Out_File(filename, locate);
  202.   if (Write_Pcx()) {
  203.     outpcxfile.close();
  204.     return -1;}
  205.   outpcxfile.close();
  206.   return 0;
  207. }
  208.  
  209. void Show_Bitmap(rcbitmap * screen, USHORT x, USHORT y)
  210. {
  211. PUCHAR imageptr=bitmap_struct->image;
  212. PUCHAR scrnptr=(screen->image+screen->width*y+x);
  213. for (LONG i=0;i<bitmap_struct->height;i++) {
  214.   for (LONG j=0;j<bitmap_struct->width;j++)
  215.     *(scrnptr++)=*(imageptr++);
  216.   scrnptr+=(SCREEN_WIDTH-bitmap_struct->width);
  217. }
  218. }
  219.